package com.yixueyun.learning.service.Impl;

import com.alibaba.fastjson.JSON;
import com.yixueyun.base.exception.YiXueYunPlusException;
import com.yixueyun.learning.config.PayNotifyConfig;
import com.yixueyun.learning.service.MyCourseTablesService;
import com.yixueyun.messagesdk.model.po.MqMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 监听队列，对消息进行处理
 */
@Slf4j
@Service
public class ReceivePayNotifyService {
    @Autowired
    private MyCourseTablesService myCourseTablesService;
    @RabbitListener(queues = PayNotifyConfig.PAYNOTIFY_QUEUE)
    public void receive(Message message) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //解析消息
        byte[] body = message.getBody();
        String StringJSON = new String(body);
        MqMessage mqmessage = JSON.parseObject(StringJSON, MqMessage.class);
        //ChooseCourseId
        String chooseCourseId = mqmessage.getBusinessKey1();
        //OrderType
        String orderType = mqmessage.getBusinessKey2();
        //判断是不是自己的消息
        //购买课程类订单消息
        if(orderType.equals("60201")) {
            //更新
            boolean b = myCourseTablesService.saveChooseCourseSuccess(chooseCourseId);
            if(!b) {
                YiXueYunPlusException.cast("保存选课状态失败");
            }
        }
    }

}
